package lumis.portal.service;

import lumis.portal.PortalException;
import lumis.portal.PortalObjectNotFoundException;
import lumis.portal.UnexpectedException;
import lumis.portal.dao.*;
import lumis.portal.dao.hibernate.GenericHibernateManualUpdateDao;
import lumis.portal.dao.hibernate.HibernateUtil;
import lumis.portal.dao.jdbc.ITransactionJdbc;
import lumis.util.ITransaction;
import lumis.util.JdbcUtil;

import java.sql.*;
import java.util.Collection;
import java.util.List;

import org.hibernate.criterion.Expression;

/**
 *  Class responsible for persisting {@link ServiceConfig} information.
 * @version $Revision: 7020 $ $Date: 2007-05-25 10:30:24 -0300 (Fri, 25 May 2007) $
 * @since 4.0.0
 */
public class ServiceDaoHib extends GenericHibernateManualUpdateDao<ServiceConfig, String> implements IServiceDao
{
	public ServiceDaoHib()
	{
		super(ServiceConfig.class);
	}
	
	protected String getIdentifier(ServiceConfig serviceConfig)
	{
		return serviceConfig.getId();
	}
	
	public void add(ServiceConfig serviceConfig, ITransaction transaction) throws DaoException, PortalException
	{
		super.add(serviceConfig, transaction);
	}

	public void update(ServiceConfig serviceConfig, ITransaction transaction) throws DaoException, PortalException
	{
		super.update(serviceConfig, transaction);
	}

	public ServiceConfig get(String serviceId, ITransaction transaction) throws DaoException, PortalException
	{
		return super.get(serviceId, transaction);
	}

	public boolean delete(String serviceId, ITransaction transaction) throws DaoException, PortalException
	{
		return super.delete(serviceId, transaction);
	}

	public Collection<String> getServicesIds(boolean getInstantiableServicesOnly, ITransaction transaction) throws DaoException, PortalException
	{
		try
		{
			// TODO filter this based on the user sessionID

			String query = "SELECT serviceId FROM lum_Service";
			
			if(getInstantiableServicesOnly)
				query += " where isInstantiable=1";

			ITransactionJdbc daoTransactionJdbc = (ITransactionJdbc) transaction;
			Connection connection = daoTransactionJdbc.getConnection();
			PreparedStatement statement = connection.prepareStatement(query);
			try
			{
				return JdbcUtil.fillListString(statement, "serviceId");
			}
			finally
			{
				statement.close();
			}
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}
	
	public ServiceConfig getByBaseUrl(String baseUrl, ITransaction daoTransaction) throws DaoException, PortalException
	{
		List<ServiceConfig> serviceConfigs = findByCriteria(daoTransaction, Expression.eq("baseUrl", baseUrl));
		if (serviceConfigs.isEmpty())
			throw new PortalObjectNotFoundException("STR_SERVICE_DOES_NOT_EXIST;" + baseUrl);
		else if (serviceConfigs.size() == 1)
			return serviceConfigs.get(0);
		else
			throw new UnexpectedException();
	}

	public void clearCache() throws PortalException
	{
		HibernateUtil.getSessionFactory().evict(ServiceConfig.class);
	}
}
